package com.api.gbhg.lbj.api;

import com.alibaba.fastjson.JSON;
import com.api.gbhg.lbj.dao.ExcelDao;
import com.api.gbhg.lbj.dao.ReimbursementDao;
import com.api.gbhg.lbj.tool.Tools;
import com.api.gbhg.lbj.util.Subsidiaryledger;
import com.api.gbhg.zengping.common.CommonDao;
import com.api.gbhg.zengping.dao.PropertyManagementCostDao;
import com.api.gbhg.zengping.web.PropertyManagementAction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lbj
 * @version 1.0
 * @description: TODO 报销导入
 * @date 2021/3/12 10:13
 */
@Path("/gbhg/excel2")
public class ReimbursementimportApi {
    private static final Logger logger = LoggerFactory.getLogger("gbhg");
    private static final ReimbursementDao reid=new ReimbursementDao();
    private   final PropertyManagementCostDao pmc=new PropertyManagementCostDao();

    @POST
    @Path("/Reimbursement")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String getExcel(Map<String,String> param){
        logger.info("测试进入报销导入excle方法--");
        Integer fileId = Integer.parseInt(param.get("file"));
        logger.info("fileId:"+fileId);
        String flowName="";
        ExcelDao excelDao=new ExcelDao();
        List<Subsidiaryledger> exceSheetindex0 = excelDao.getExceSheetindex0(fileId);//报销导入excle
        Map<String,String>[] list=new HashMap[exceSheetindex0.size()];
        Map<String, String> map=new HashMap<>();
        for (int i=0;i<exceSheetindex0.size();i++){
            list[i]=new HashMap<>();
            String xmdm = exceSheetindex0.get(i).getAuxiliaryitems();//辅助项
            xmdm=xmdm.substring(xmdm.indexOf("目")+2);//项目代码 id
            String bbjjzcfl = exceSheetindex0.get(i).getSectoreconomicclassification();//部门经济分类
            bbjjzcfl=bbjjzcfl.substring(bbjjzcfl.indexOf("类")+2,bbjjzcfl.indexOf("_"));
            String bxje = exceSheetindex0.get(i).getReimbursementamount();//报销金额
            String excelXmmc = exceSheetindex0.get(i).getEntryname();//项目名称
            String remark = exceSheetindex0.get(i).getRemark();//备注
            logger.info("项目代码:"+xmdm+"部门经济分类:"+bbjjzcfl+"报销金额:"+bxje);
            RecordSet getxmid = Tools.getxmid(xmdm);
            getxmid.next();
            String xmxx = getxmid.getString("lcjlsqjlid");//项目信息
            logger.info("项目信息:"+xmxx);
            list[i].put("lcjlid",xmxx);
            list[i].put("bmzcjjfl",bbjjzcfl);
            list[i].put("xmmc",excelXmmc);
            list[i].put("money",bxje);
            list[i].put("remark",remark);
            reid.addDetail(list,flowName); //uf_jingfei_dt1 新增报销记录
            boolean b = false;
            map = new HashMap<>();
            try {
                b = reid.queryJingfei1(list,map);
            } catch (Exception e) {
                e.printStackTrace();
            }
            boolean flag=true;
            //boolean flag = reid.jingfeilaiyuanExcl(list,map);
            //添加报销流程明细记录
            Map<String,Object> data=new HashMap<>();
            //mainid
            data.put("mainid","");
            //报销金额
            data.put("bxje",bxje);
            //报销流程id
            data.put("bxlcid","");
            //项目流程id
            data.put("xmlcid",xmxx);
            //项目流程名称  根据 ysxm 查==>查经费表
            RecordSet rsj = CommonDao.selectJingfei(xmxx);
            rsj.next();
            //项目名称
            String xmmc = rsj.getString("xmmc");
            data.put("xmlcmc",xmmc);
            //部门经济支出分类
            data.put("bmjjzcfl",bbjjzcfl);
            //政府经济支出分类
            data.put("zfjjzcfl","");
            //备注
            data.put("bz",remark);
            logger.info("报销导入excel data----:"+data.toString());
            boolean b2 = pmc.addbxDetailRecord(data);
            if(!b||!flag||!b2){
                map.put("success","error");
                map.put("msg","报销失败");
            }else{
                map.put("success","ok");
                map.put("msg","报销成功");
            }
        }
        return JSON.toJSONString(map);
    }
}
